﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Resco.Controls.DetailView;

namespace AsTekMobile
{
    public partial class frmArac : Form
    {
        public string AracKodu { get; set; }
        private AracBilgi SecilenArac;
        public frmArac()
        {
            InitializeComponent();
        }

        private void frmArac_Load(object sender, EventArgs e)
        {
            try
            {
                LoadArac();
            }
            catch (Exception ex)
            {
                ErrorMgr.Opps(ex);
            }
        }

        private void LoadArac()
        {
            if (!string.IsNullOrEmpty(AracKodu))
            {
                foreach (var item in Database.ctor.FirmaListesi)
                {
                    cmbFirma.Add(item);
                }
                cmbFirma.DisplayMember = "CariUnvan1";

                foreach (var item in Database.ctor.AnaCariListe)
                {
                    cmbGrup.Add(item);
                }
                cmbGrup.DisplayMember = "CariUnvan1";

                foreach (var item in Database.ctor.Markalar)
                {
                    cmbMarka.Add(item);
                }
                cmbMarka.DisplayMember = "MarkaAdi";

                foreach (var item in Database.ctor.SistemTanimlari)
                {
                    cmbSistem.Add(item);
                }
                cmbSistem.DisplayMember = "Adi";

                foreach (var item in Database.ctor.FirmwareTanimlari)
                {
                    cmbFirmware.Add(item);
                }
                cmbFirmware.DisplayMember = "Adi";

                AracBilgi arc = Database.ctor.AracBilgileri(AracKodu);
                if (arc != null)
                {
                    SecilenArac = arc;
                    for (int i = 0; i < cmbFirma.Items.Length; i++)
                    {
                        if (((CariBilgi)cmbFirma.Items[i]).CariKodu == arc.AltCariKodu)
                        {
                            cmbFirma.SelectedIndex = i;
                            break;
                        }
                    }

                    for (int i = 0; i < cmbGrup.Items.Length; i++)
                    {
                        if (((CariBilgi)cmbGrup.Items[i]).CariKodu == arc.AnaCariKodu)
                        {
                            cmbGrup.SelectedIndex = i;
                            break;
                        }
                    }

                    for (int i = 0; i < cmbMarka.Items.Length; i++)
                    {
                        if (((Marka)cmbMarka.Items[i]).ID == arc.MarkaID)
                        {
                            cmbMarka.SelectedIndex = i;
                            break;
                        }
                    }

                    for (int i = 0; i < cmbSistem.Items.Length; i++)
                    {
                        if (((Sistem)cmbSistem.Items[i]).ID == arc.SistemID)
                        {
                            cmbSistem.SelectedIndex = i;
                            break;
                        }
                    }

                    for (int i = 0; i < cmbFirmware.Items.Length; i++)
                    {
                        if (((Firmware)cmbFirmware.Items[i]).ID == arc.FirmwareID)
                        {
                            cmbFirmware.SelectedIndex = i;
                            break;
                        }
                    }

                    txtPlaka.Text = arc.Plaka;
                    txtYil.NumericValue = arc.UretimYili.Year;
                    txtSasi.Text = arc.SasiNo;
                    txtKoltuk.Text = arc.KoltukSayisi.ToString();
                    txtSistemNo.Text = arc.SistemNo;


                    var ozellik = Database.ctor.OzellikTanimlari;
                    var aracoz = Database.ctor.AracOzellikleri(SecilenArac.ID);
                    int idx = 1;
                    foreach (var item in ozellik)
                    {
                        for (int i = 0; i < detailView1.Items.Count; i++)
                        {
                            if (detailView1.Items[i].Name == "txtEkOzellik" + idx)
                            {
                                ItemComboBox txt = (ItemComboBox)detailView1.Items[i];
                                idx++;
                                AracOzellik oz = aracoz.Where(x => x.OzellikID == item.ID).FirstOrDefault();
                                if (oz != null)
                                {
                                    if (item.Adi.IndexOf(':') > -1)
                                    {
                                        txt.Label = item.Adi.Split(':')[0].Trim();
                                        string[] dizi = item.Adi.Split(':')[1].Trim().Split(',');
                                        for (int x = 0; x < dizi.Length; x++)
                                        {
                                            txt.Add(dizi[x]);
                                        }
                                    }
                                    else
                                    {
                                        txt.Label = item.Adi;
                                        txt.Add(oz.Deger);
                                    }
                                    txt.Text = oz.Deger;
                                    txt.Tag = oz;
                                }
                                else
                                {
                                    if (item.Adi.IndexOf(':') > -1)
                                    {
                                        txt.Label = item.Adi.Split(':')[0].Trim();
                                        string[] dizi = item.Adi.Split(':')[1].Trim().Split(',');
                                        for (int x = 0; x < dizi.Length; x++)
                                        {
                                            txt.Add(dizi[x]);
                                        }
                                    }
                                    else
                                    {
                                        txt.Label = item.Adi;
                                        txt.Add("");
                                    }
                                    txt.Text = "";
                                    txt.Tag = new AracOzellik() { Deger = "", ID = -1, OzellikID = item.ID };
                                }
                                break;
                            }
                        }
                    }

                    var hubs = Database.ctor.AracHubYerleri(SecilenArac.ID);
                    foreach (var item in hubs)
                    {
                        foreach (Item c in detailView1.Items)
                        {
                            if (c.Name == "txtHub" + item.HubKoltukNo)
                            {
                                ItemMaskedTextBox txt = (ItemMaskedTextBox)c;
                                txt.Label = item.HubKoltukNo.ToString();
                                txt.Text = item.MonitorKoltukNo1.ToString("00") + "-" + item.MonitorKoltukNo2.ToString("00") + "-" + item.MonitorKoltukNo3.ToString("00") + "-" + item.MonitorKoltukNo4.ToString("00") + "-" + item.MonitorKoltukNo5.ToString("00") + "-" + item.MonitorKoltukNo6.ToString("00") + "-" + item.MonitorKoltukNo7.ToString("00");
                                txt.Tag = item.ID;
                            }
                        }
                    }

                }
            }
        }

        private void SaveArac()
        {
            if (SecilenArac != null)
            {
                if (cmbFirma.SelectedIndex > -1)
                {
                    SecilenArac.AltCariKodu = ((CariBilgi)cmbFirma.SelectedItem).CariKodu;
                }
                if (cmbGrup.SelectedIndex > -1)
                {
                    SecilenArac.AnaCariKodu = ((CariBilgi)cmbGrup.SelectedItem).CariKodu;
                }
                if (cmbMarka.SelectedIndex > -1)
                {
                    SecilenArac.MarkaID = ((Marka)cmbMarka.SelectedItem).ID;
                }
                if (cmbSistem.SelectedIndex > -1)
                {
                    SecilenArac.SistemID = ((Sistem)cmbSistem.SelectedItem).ID;
                }
                if (cmbFirmware.SelectedIndex > -1)
                {
                    SecilenArac.FirmwareID = ((Firmware)cmbFirmware.SelectedItem).ID;
                }

                if (!string.IsNullOrEmpty(txtPlaka.Text))
                    SecilenArac.Plaka = txtPlaka.Text;
                if (!string.IsNullOrEmpty(txtYil.Text))
                    SecilenArac.UretimYili = new DateTime(Cevir.Parse<int>(txtYil.Text), 1, 1);
                if (!string.IsNullOrEmpty(txtSasi.Text))
                    SecilenArac.SasiNo = txtSasi.Text;
                if (!string.IsNullOrEmpty(txtKoltuk.Text))
                    SecilenArac.KoltukSayisi = Cevir.Parse<int>(txtKoltuk.Text);
                if (!string.IsNullOrEmpty(txtSistemNo.Text))
                    SecilenArac.SistemNo = txtSistemNo.Text;

                int idx = 1;
                for (int i = 0; i < detailView1.Items.Count; i++)
                {
                    if (detailView1.Items[i].Name == "txtEkOzellik" + idx)
                    {
                        ItemComboBox txt = (ItemComboBox)detailView1.Items[i];
                        if (txt.Tag != null)
                        {
                            AracOzellik oz = (AracOzellik)txt.Tag;
                            SecilenArac.EkOzellikler.Add(new AracOzellik()
                            {
                                Deger = txt.Text,
                                ID = oz.ID,
                                OzellikID = oz.OzellikID
                            });
                        }
                        idx++;
                    }
                }

                idx = 1;
                foreach (Item c in detailView1.Items)
                {
                    if (c.Name == "txtHub" + idx)
                    {
                        ItemMaskedTextBox txt = (ItemMaskedTextBox)c;
                        string[] dizi = txt.Text.Split('-');
                        SecilenArac.HubYerleri.Add(new HubYeri()
                        {
                            ID = Cevir.Parse<int>(txt.Tag),
                            HubKoltukNo = idx,
                            MonitorKoltukNo1 = Cevir.Parse<int>(dizi[0]),
                            MonitorKoltukNo2 = Cevir.Parse<int>(dizi[1]),
                            MonitorKoltukNo3 = Cevir.Parse<int>(dizi[2]),
                            MonitorKoltukNo4 = Cevir.Parse<int>(dizi[3]),
                            MonitorKoltukNo5 = Cevir.Parse<int>(dizi[4]),
                            MonitorKoltukNo6 = Cevir.Parse<int>(dizi[5]),
                            MonitorKoltukNo7 = Cevir.Parse<int>(dizi[6]),
                        });
                        idx++;
                    }
                }

                if (Database.ctor.SaveArac(SecilenArac))
                    MessageBox.Show("Kaydedildi !");
                else
                    MessageBox.Show("Kaydedilmedi !");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult rs = MessageBox.Show("Araç Bilgileri güncellenecektir.Emin misiniz ?", "Uyarı", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
            if (rs == DialogResult.Yes)
            {
                SaveArac();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (Sip.Visible)
            {
                Sip.Visible = false;
            }
            else
            {
                Sip.Visible = true;
            }
        }
    }
}